package com.smartrobot.smartrobot.service;


import com.smartrobot.smartrobot.R;
import com.smartrobot.smartrobot.activity.CretaOrganActivity;

import android.annotation.SuppressLint;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.app.TaskStackBuilder;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Binder;
import android.os.IBinder;
import android.support.v4.app.NotificationCompat;
import android.util.Log;

public class SmartRobotService extends Service{
	
	private final String TAG = "SmartRobotService ---------->  ";
	public ServerBinder binder = new ServerBinder();
	public boolean isBind = false;
	private final String OLDER_UPDATE = "Update";
	
	/**
	 * 只会在第一次创建才会触发
	 */
	public void onCreate() {
		super.onCreate();
		Log.d(TAG, "onCreate() executed"); 
	}
	
	/**
	 * 每次都会触发
	 */
	public int onStartCommand(Intent intent, int flags, int startId) {
		
		Log.d(TAG, "onStartCommand() executed");
		
		if( intent != null && intent.getStringExtra("older") != null  && intent.getStringExtra("older").equals(OLDER_UPDATE)){
			System.out.println("服务器发来指令---->");
			binder.msgFromBrocast();
		}
		
		
		return super.onStartCommand(intent, flags, startId);
		 
	}

	@Override
	public IBinder onBind(Intent intent) {
		
		Log.d(TAG, "IBinder() executed"); 
		isBind = true;
		return binder;
	}
	
	
	@Override
	public void unbindService(ServiceConnection conn) {
		
		if(isBind){
			Log.d(TAG, "unBinder() executed"); 
			super.unbindService(conn);
			isBind = false;
		}
		
		
	}

	/**
	 * 销毁的时候触发
	 */
	public void onDestroy() {
		
		Log.d(TAG, "onDestroy() executed");
		
		binder = null;
		
		super.onDestroy();
	}
	
	/**
	 * 建立具体绑定 实现activity和service通讯
     * 一旦调用bindService方法那么service就与 activity共存亡
	 */
	public class ServerBinder extends Binder{
		
		public void startDownload(){
			 Log.d("TAG", "startDownload() executed"); 
		}
		
		/**
		 * 弹出通知
		 */
		@SuppressLint("NewApi")
		public void showNotification(int time){

            NotificationCompat.Builder mBuilder =
			        new NotificationCompat.Builder(SmartRobotService.this)
			        .setTicker("测试")
			        .setWhen(System.currentTimeMillis())
			        .setSmallIcon(R.drawable.ic_launcher)
			        .setContentTitle("这个是标题")
			        .setContentText("这个是时间    "+time)
			        .setAutoCancel(true)//点击后自动关闭
			        ;
//			点击 状态栏后跳转到 那个app去
			Intent resultIntent = new Intent(SmartRobotService.this, CretaOrganActivity.class);
			
			TaskStackBuilder stackBuilder = TaskStackBuilder.create(SmartRobotService.this);
			stackBuilder.addParentStack(CretaOrganActivity.class);
			
			
			stackBuilder.addNextIntent(resultIntent);
			
			PendingIntent resultPendingIntent =
			        stackBuilder.getPendingIntent(
			            0,
			            PendingIntent.FLAG_CANCEL_CURRENT
			        );
			
			mBuilder.setContentIntent(resultPendingIntent);
			NotificationManager mNotificationManager =
			    (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
			

			mNotificationManager.cancel(R.id.startServer);
			mNotificationManager.notify(R.id.startServer, mBuilder.build());
			
		}
		
		
		public void sendBrocastCast(){
			
			Intent intent = new Intent("com.brocast.test");
			sendBroadcast(intent);
			
			
		}
		
		public void msgFromBrocast(){
			
			System.out.println("接受广播");
			
		}
		
		
	}
	
	
}
